package DBUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {

//		private static  String url = "jdbc:mysql://47.106.206.66:3306/my_test2?useUnicode=true&characterEncoding=utf-8&useSSL=false";
//		private static String username = "root1";
//		private static String password = "root";

	private static String url = "jdbc:oracle:thin:@192.168.0.168:1521:orcl";
	private static String username = "dsci_gs";
	private static String password = "dsci_gs";


	/**
	 * 数据库连接操作
	 * 
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception {

		// 使用jdbc链接数据库
//		Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

		// 获取数据库连接
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}

	// 执行关闭流的操作
	public static void close(ResultSet rs, Statement stmt, Connection conn) throws SQLException {
		rs.close();
		stmt.close();
		conn.close();
	}
	
	public static void close(Statement stmt, Connection conn) throws SQLException {
		stmt.close();
		conn.close();
	}
	
	
	public static ResultSet execte (String sql) throws Exception{
		Connection conn = getConnection();
		Statement stmt = conn.createStatement();
		//String sql = "select * from test limit 1000000";
		ResultSet rs = null;
		if (sql .contains("select"))
			rs = stmt.executeQuery(sql);
		else if (sql.contains("insert"))
			stmt.execute(sql);
		
		return rs; 
	}
	

}
